﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using Microsoft.Http;
using System.Xml.Linq;
using System.Web;
using System.Web.Script.Services;

namespace Woodbase.CodeBase.UI
{
    public class WoodbaseBasePage : System.Web.UI.Page
    {
        [WebMethod]
        public static void SaveXml(string userid, string titles)
        {
            using (HttpClient client = new HttpClient("http://localhost:9090/WVRServices/"))
            {
                XDocument x = XDocument.Parse("<titles>" + titles + "</titles>");
                StringBuilder sTitles = new StringBuilder();
                foreach (var t in x.Descendants("li"))
                {
                    sTitles.Append(t.Value + ";");
                }

                using (HttpResponseMessage msg = client.Post("User/" + userid + "/Movie/Rent/" + HttpUtility.UrlEncode(sTitles.ToString()), HttpContent.CreateEmpty()))
                {
                    msg.EnsureStatusIsSuccessful();
                }
            }
        }

        [WebMethod]
        [ScriptMethod]
        public static void RemoveFilm(string userid, string title)
        {
            using (HttpClient client = new HttpClient("http://localhost:9090/WVRServices/"))
            {
                using(HttpResponseMessage msg = client.Delete("User/"+userid+"/Movie/Return/"+title))
                {
                    msg.EnsureStatusIsSuccessful();
                }
            }
        }
    }
}
